//
//  DanquViewController.m
//  艾听歌
//
//  Created by dllo on 16/5/28.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "DanquViewController.h"
#import "DanquTableViewCell.h"
#import "MusicPlyController.h"
#import "UrlListModel.h"

#import "SingerViewController.h"
//#import "SongModel.h"
#import "SongListModel.h"

@interface DanquViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) BOOL isUpLoad;


@end

@implementation DanquViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor redColor];
    self.view.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorPickerWithRGB(0xFF6EB4, 0x9C9C9C);
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tocolletionDetail:) name:@"playSongs" object:nil];
    self.dataArr = [NSMutableArray array];
    
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
//    NSLog(@"%@",self.mId);
    
    [self createTableView];
//    [self dataHandle:self.page];
    [self addHeader];
    [self addHeader];
    
}
- (void)addHeader
{
    __block DanquViewController *root = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        root.page = 1;
        root.isUpLoad = NO;
        [root dataHandle:root.page];
        
    }];
    [self.tableView.mj_header beginRefreshing];
}
- (void)addFooter
{
    __block DanquViewController *root = self;
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        root.isUpLoad = YES;
        root.page++;
        [root dataHandle:root.page];
        
    }];
}


- (void)dataHandle:(NSInteger)nextCurse
{
    
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *str = [NSString stringWithFormat:@"http://api.dongting.com/song/singer/%@/songs?page=%ld&size=50&app=ttpod&v=v9.0.1.2016050614&user_id=0&mid=iPhone7%%2C2&f=f320&s=s310&imsi=&hid=&splus=9.3.1&active=1&net=2&openudid=5e6106cc8d5bcf35f60a2997c79a00bb5a7c631f&idfa=ABDADD65-0877-4840-B14F-DFC8D1114AB0&utdid=VzR0dwnWYs8DANbYJ8SsFzOU&alf=(null)&bundle_id=com.ttpod.music&latitude=-1&longtitude=-1&access_token=(null)&vc=9000100",self.mId, nextCurse];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        [netWorkManager stopMonitoring];
        if (self.isUpLoad == NO) {
            [self.dataArr removeAllObjects];
        }
        NSMutableArray *array = [responseObject objectForKey:@"data"];
        
        for (NSDictionary *dic in array) {
            SongListModel *model = [[SongListModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArr addObject:model];
        }
       
        
        if (nextCurse == 1) {
            [self.dataArr removeObjectAtIndex:0];
        }
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
       
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }];
}
    
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT ) style:UITableViewStylePlain];
//    self.tableView.backgroundColor = [UIColor redColor];
    self.tableView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 60;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[DanquTableViewCell class] forCellReuseIdentifier:@"singleCell"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DanquTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"singleCell" forIndexPath:indexPath];
    
    cell.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    
    SongListModel *songmodel = self.dataArr[indexPath.row];
    cell.numLabel.text = [@(indexPath.row + 1) stringValue];
    cell.songName.text = songmodel.name;
    cell.singerName.text = songmodel.singerName;
     NSInteger count = [songmodel.favorites floatValue];
    NSString *str = nil;
    if (count >= 10000) {
        CGFloat num = count / 10000.0;
        if (count % 10000 >= 10000.0) {
            str = [NSString stringWithFormat:@"%.1f 万",num];
            
        }else {
            str = [NSString stringWithFormat:@"%.0f 万", num];
            
        }
    }else{
        str = [NSString stringWithFormat:@"%ld",count];
    }
    cell.favorites.text = str;
    
    return cell;
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    MusicPlyController *MusicPlay = [MusicPlyController musicplyVC];
//    
//    NSMutableArray *newArray = [NSMutableArray array];
//    
//    for (SongListModel *listModel in self.dataArr) {
//        NSLog(@"%@",self.dataArr);
//        UrlListModel *urlModel = [listModel.urlList firstObject];
//        
//        [newArray addObject:urlModel.url];
//    }
//    
//    NSLog(@"%@", newArray);
//    MusicPlay.MusicArray = [NSMutableArray arrayWithArray:newArray];
//    MusicPlay.index = indexPath.row;
//    
//    
//    [self presentViewController:MusicPlay animated:YES completion:^{
//        
//        
//    }];
//    
//}




@end
